package com.car.dao.impl;


import com.car.dao.PurchaseRecordDao;
import com.car.domain.PurchaseRecord;
import com.car.utils.JDBCUtil;

import java.util.List;

/**
 * PurchaseRecordDao的实现类
 */
public class PurchaseRecordDaoImpl implements PurchaseRecordDao {
    /**
     * 新增
     *
     * @param pr 泛型
     */
    @Override
    public int add(PurchaseRecord pr) throws Exception {
        String sql = "insert into t_purchase_record (user_id, car_id, employee_id, purchase_datetime) \n" +
                "VALUE(?,?,?,?)";
        return JDBCUtil.update(sql, pr.getUserId(), pr.getCarId(), pr.getEmployeeId(), pr.getPurchaseDatetime());
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return 影响记录数
     */
    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from t_purchase_record where id = ? ";
        return JDBCUtil.update(sql, id);
    }

    /**
     * 修改
     *
     * @param pr 泛型
     */
    @Override
    public int update(PurchaseRecord pr) throws Exception {
        String sql = "update t_purchase_record set user_id = ?,car_id=?," +
                "employee_id=?,purchase_datetime=? " +
                "where id = ?";
        return JDBCUtil.update(sql, pr.getUserId(), pr.getCarId(), pr.getEmployeeId()
                , pr.getPurchaseDatetime(), pr.getId());
    }

    /**
     * 通过主键id查询
     *
     * @param id 主键id
     * @return 实体类
     */
    @Override
    public PurchaseRecord queryById(int id) throws Exception {
        String sql = "select * from t_purchase_record where id = ?";
        return JDBCUtil.query(sql, PurchaseRecord.class, id);
    }

    /**
     * 查询所有记录
     *
     * @return 实体类集合
     */
    @Override
    public List<PurchaseRecord> queryAll() throws Exception {
        String sql = "select * from t_purchase_record";
        return JDBCUtil.queryList(sql, PurchaseRecord.class);
    }

    @Override
    public List<PurchaseRecord> findByUserId(Integer userId) throws Exception {
        String sql = "select * from t_purchase_record where user_id = ?";
        return JDBCUtil.queryList(sql, PurchaseRecord.class, userId);
    }
}
